import axios from 'axios'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { Message } from 'element-ui'

const request = axios.create({
    baseURL: 'http://admin.taotaozi.ronghanbing.com/api/admin/',
    // baseURL: 'http://172.19.8.78:8090/admin/',
    // baseURL: 'http://101.43.64.8:8890/admin/',
    // baseURL: 'http://172.19.58.191:8890/admin/',
    timeout: 5000
})

let CancelToken = axios.CancelToken

// 请求拦截
request.interceptors.request.use(config => {
        //对于访客模式，除GET请求外，都拦截并提示
        const userJson = window.localStorage.getItem('user') || '{}'
        const user = JSON.parse(userJson)
        if (userJson !== '{}' && user.role !== 'ROLE_admin' && config.method !== 'get') {
            config.cancelToken = new CancelToken(function executor(cancel) {
                cancel('演示模式，不允许操作')
            })
            return config
        }

        NProgress.start()
        const token = window.localStorage.getItem('token')
        if (token) {
            config.headers.Authorization = token
        }
        return config
    },
    error => {
        console.info(error)
        return Promise.reject(error)
    }
)

// 响应拦截
request.interceptors.response.use(response => {
        NProgress.done()
        const res = response.data
        if (res.code !== 200) {
            Message.error(res.msg || 'Error')
        }
        return res
    },
    error => {
        console.info(error)
        Message.error(error.message)
        return Promise.reject(error)
    }
)

export default request